﻿@using Models
@model Models.News

@{
    ViewBag.Title = "Update news";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<link href="/Content/start/jquery-ui-1.8.21.custom.css" rel="stylesheet" type="text/css"/>
<style>
	#sortable1, #sortable2 { list-style-type: none; margin: 0; padding: 0 0 2.5em; float: left; margin-right: 10px; }
	#sortable1 li, #sortable2 li { margin: 0 5px 5px 5px; padding: 5px; font-size: 1.2em; width: 120px; }
	</style>
  <script src="/Scripts/jquery-1.7.2.min.js"></script>
  <script src="/Scripts/jquery-ui-1.8.21.custom.min.js"></script>
<script>
    $(function () {
        $("#sortable1, #sortable2").sortable(
	            {
	                remove: function () {
	                    var tags = document.getElementById('tags');
	                    tags.value = "";
	                    var ui = document.getElementById('sortable2');
	                    var liNodes = ui.getElementsByTagName('li');
	                    for (var i = 0; i < liNodes.length; i++) {
	                        if (tags.value == "")
	                            tags.value = liNodes[i].id;
	                        else {
	                            tags.value += ',' + liNodes[i].id;
	                        }


	                    }
	                    // alert(tags.value);
	                },
	                connectWith: '.otherlist'
	            },
	            {
	                dropOnEmpty: true
	            },
	            { scroll: false }


	    );
    });

	        var currentFielsIndex = 1;
	        function addField() {
	            currentFielsIndex++;
	            var file = document.createElement("div");
	            file.innerHTML = "<input name=\"uploadFiles\" type=\"file\" /> <a onclick=\"return deleteField(this)\" href=\"#\">[X]</a><br/>";
	            document.getElementById("filesToUpload").appendChild(file);
	            return false;
	        }

	        function deleteField(a) {
	            var div = a.parentNode;

	            div.innerHTML = "";
	            return false;
	        }
	        
	        var checkedList = [];
	        function formDeleteImages(that) {
	           
	            var imagesToDelete = document.getElementById('imagesToDelete');
	            imagesToDelete.value = "";
            if (that.checked) {
                checkedList.push(that.value);
            } else {
                 for(var i = 0; i < checkedList.length; i++) {
                    if(checkedList[i] == that.value) {
                        checkedList.pop(i);
                    }
                }
            }
            for (var k = 0; k < checkedList.length; k++ ) {
                if (imagesToDelete.value == "") {
                    imagesToDelete.value = checkedList[k];
                } else {
                    imagesToDelete.value = imagesToDelete.value + ',' + checkedList[k];
                }
            }
	    
            return false;
        }


	</script>

<div style="float: left; height: 70px"><a href="/admin/index" ><img src="/Content/images/back.png" /></a></div>
<div style="margin-left: 80px;"><a href="/admin/AllNews" ><img src="/content/images/news_all.png" alt="All News" /><br />All news</a></div>


<h2>Update news </h2>

@if (ViewBag.Message != null)
{
    
    <b> @ViewBag.Message</b> 
}
else
{

    using (Html.BeginForm("UpdateNews", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
     @Html.ValidationSummary()
     <table id="table_admin">
         <tr>
             <td>
                 @Html.LabelFor(x => x.Title)
             </td>
             <td>
                 @Html.TextBoxFor(x => x.Title, new { value = Model.Title })
             </td>
         </tr>
         <tr>
             <td>
                 @Html.LabelFor(x => x.Text)
             </td>
             <td>
                 @Html.TextAreaFor(x => x.Text, new { value = Model.Text })
             </td>
         </tr>
         <tr>
             <td>
                 @Html.LabelFor(x => x.Category)
             </td>
             <td>
                 @{
        List<Category> drlist = ViewBag.Categories;
                     @Html.DropDownListFor(x => x.Category, new SelectList(drlist, "Id", "Name"))
                 }
             </td>
         </tr>
         <tr>
             <td>
                 Tags
                 
             </td>
             <td>
                 @{
        List<Tag> tags = ViewBag.Tags;
        List<Tag> selectedTags = ViewBag.selectedTags;

        for (int i = 0; i < tags.Count; i++)
        {
            foreach (Tag selectedTag in selectedTags)
            {
                if (tags[i].Id == selectedTag.Id)
                {
                    tags.RemoveAt(i);
                }
            }
        }
                        
                 }
                 <ul id="sortable1" class="otherlist">
                     @foreach (Tag tag in tags)
                     {
                         <li id="@tag.Id" class="ui-state-default">@tag.Name</li>
                     }
                 </ul>
                   
                 <ul id="sortable2" class="otherlist" >
                     @foreach (Tag tag in selectedTags)
                     {
                         <li id="@tag.Id" class="ui-state-active">@tag.Name</li>
                     } 
                 </ul>
                
             </td>
         </tr>
         <tr>
             <td colspan="2" align="center">
                 <b>Attached images</b>
             </td>
         </tr>
         @if(Model.Images != null)
         {
             for (int i = 0; i < Model.Images.Count; i++)
              {
                  <tr>
                      <td>
                          <img src="@Model.Images[i].Url" alt="@Model.Images[i].Name" id="image_in_news"/>
                      </td>
                      <td>
                          <input type="checkbox" id="@Model.Images[i].Id" value="@Model.Images[i].Id" onchange="return formDeleteImages(this)"/>
                      </td>
                  </tr>
              }
         }
         
         <tr>
                <td colspan="2">
                    <div id="filesToUpload">
                    <input type="file" name="uploadFiles"/> <br/>

                    </div>
                    <a href="#" onclick="return addField()">Add fields</a>
                </td>
            </tr>

         <tr>
             <td colspan="2">
                 
                 @Html.HiddenFor(x => x.UpdateDate)
                 @Html.HiddenFor(x => x.PublishDate)
                 @Html.HiddenFor(x => x.Author)
                 @Html.HiddenFor(x => x.Id)
                 @Html.Hidden("tags", "")
                 
                 @Html.Hidden("imagesToDelete", "")
                 <input type="submit" name="subupdate" value="Update" />
             </td>
         </tr>
     </table>

 }
}